<?php

namespace App\Events;

use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\Models\SystemNotify as SysNotify;

class SystemNotify extends Event implements ShouldBroadcast
{
    use SerializesModels;
    public $user_id;
    public $message;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($user_id,$message)
    {
        //
        $this->user_id = $user_id;
        $this->message = $message;
        $sysnotify = new SysNotify();
        $sysnotify->user_id = $this->user_id;
        $sysnotify->message = $this->message;
        $sysnotify->save();
        require (public_path('/').'../vendor/pusher/pusher-php-server/lib/Pusher.php');
        $options = array(
            'cluster' => 'ap1',
            'encrypted' => true
        );
        $pusher = new \Pusher(
            'fd3d714be0dc78293442',
            '797c5d5af7ed84f1d7f8',
            '330335',
            $options
        );
        $data['user_id'] = $this->user_id;
        $data['message'] = $this->message;
        $pusher->trigger($this->user_id, 'my-event', $data);
    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        /*$sysnotify = new SysNotify();
        $sysnotify->user_id = $this->user_id;
        $sysnotify->message = $this->message;
        $sysnotify->save();
        require (public_path('/').'../vendor/pusher/pusher-php-server/lib/Pusher.php');
        $options = array(
            'cluster' => 'ap1',
            'encrypted' => true
        );
        $pusher = new \Pusher(
            'fd3d714be0dc78293442',
            '797c5d5af7ed84f1d7f8',
            '330335',
            $options
        );
        $data['user_id'] = $this->user_id;
        $data['message'] = $this->message;
        $pusher->trigger('my-channel', 'my-event', $data);*/
        return ['laravel'];
    }
}
